{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a8b892c8",
   "metadata": {},
   "source": [
    "Printed and electronic copies of *Modeling and Simulation in Python* are available from [No Starch Press](https://nostarch.com/modeling-and-simulation-python) and [Bookshop.org](https://bookshop.org/p/books/modeling-and-simulation-in-python-allen-b-downey/17836697?ean=9781718502161) and [Amazon](https://amzn.to/3y9UxNb)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "early-drove",
   "metadata": {},
   "source": [
    "# Case Studies Part 3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "imported-table",
   "metadata": {
    "tags": []
   },
   "source": [
    "*Modeling and Simulation in Python*\n",
    "\n",
    "Copyright 2021 Allen Downey\n",
    "\n",
    "License: [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-nc-sa/4.0/)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acoustic-small",
   "metadata": {},
   "source": [
    "This chapter is a collection of case studies you might want to read and work on.\n",
    "They are based on the methods in the last few chapters, including Newtonian mechanics in 1-D and 2-D, and rotation around a single axis."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "capital-vatican",
   "metadata": {},
   "source": [
    "## Bungee Jumping\n",
    "\n",
    "Suppose you want to set the world record for the highest \"bungee dunk\", which is a stunt in which a bungee jumper dunks a cookie in a cup of tea at the lowest point of a jump. An example is shown in this video: <http://modsimpy.com/dunk>.\n",
    "\n",
    "Since the record is 70 m, let's design a jump for 80 m. We'll start with the following modeling assumptions:\n",
    "\n",
    "-   Initially the bungee cord hangs from a crane with the attachment\n",
    "    point 80 m above a cup of tea.\n",
    "\n",
    "-   Until the cord is fully extended, it applies no force to the jumper.  It turns out this might not be a good assumption; we'll revisit it in the next case study.\n",
    "\n",
    "-   After the cord is fully extended, it obeys Hooke's Law; that is, it applies a force to the jumper proportional to the extension of the cord beyond its resting length. See <http://modsimpy.com/hooke>.\n",
    "\n",
    "-   The mass of the jumper is 75 kg.\n",
    "\n",
    "-   The jumper is subject to drag force so that their terminal velocity is 60 m/s.\n",
    "\n",
    "Our objective is to choose the length of the cord, `L`, and its spring\n",
    "constant, `k`, so that the jumper falls all the way to the tea cup, but no farther!\n",
    "\n",
    "In the repository for this book, you will find a notebook,\n",
    "*bungee1.ipynb*, which contains starter code and exercises for this case study.\n",
    "You can download it from <https://github.com/AllenDowney/ModSimPy/raw/master/examples/bungee1.ipynb> or run it on Colab at <https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/examples/bungee1.ipynb>."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "recognized-bidding",
   "metadata": {},
   "source": [
    "## Bungee Dunk Revisited\n",
    "\n",
    "In the previous case study, we assume that the cord applies no force to\n",
    "the jumper until it is stretched. It is tempting to say that the cord\n",
    "has no effect because it falls along with the jumper, but that intuition\n",
    "is incorrect. As the cord falls, it transfers energy to the jumper.\n",
    "\n",
    "At <http://modsimpy.com/bungee> you'll find a paper (Heck, Uylings, and Kędzierska, \"Understanding the physics of bungee jumping\", Physics Education, Volume 45, Number 1, 2010.) that explains\n",
    "this phenomenon and derives the acceleration of the jumper, $a$, as a\n",
    "function of position, $y$, and velocity, $v$:\n",
    "\n",
    "$$a = g + \\frac{\\mu v^2/2}{\\mu(L+y) + 2L}$$ \n",
    "\n",
    "where $g$ is acceleration due to gravity, $L$ is the length of the cord, and $\\mu$ is the ratio of the mass of the cord, $m$, and the mass of the jumper, $M$.\n",
    "\n",
    "If you don't believe that their model is correct, this video might\n",
    "convince you: <http://modsimpy.com/drop>.\n",
    "\n",
    "In the repository for this book, you will find a notebook,\n",
    "*bungee2.ipynb*, which contains starter code and exercises for this case study. \n",
    "You can download it from <https://github.com/AllenDowney/ModSimPy/raw/master/examples/bungee2.ipynb> or run it on Colab at <https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/examples/bungee2.ipynb>.\n",
    "\n",
    "How does the behavior of the system change as we vary the mass of the cord? When the mass of the cord equals the mass of the jumper, what is the net effect on the lowest point in the jump?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "german-penetration",
   "metadata": {},
   "source": [
    "## Orbiting the Sun\n",
    "\n",
    "In the exercise at the end of Chapter 20, we modeled the interaction between the Earth and the Sun, simulating what would happen if the Earth stopped in its orbit and fell straight into the Sun.\n",
    "\n",
    "Now let's extend the model to two dimensions and simulate one revolution of the Earth around the Sun, that is, one year.\n",
    "\n",
    "In the repository for this book, you will find a notebook,\n",
    "*orbit.ipynb*, which contains starter code and exercises for this case study.\n",
    "You can download it from <https://github.com/AllenDowney/ModSimPy/raw/master/examples/orbit.ipynb> or run it on Colab at <https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/examples/orbit.ipynb>.\n",
    "\n",
    "Among other things, you will have a chance to experiment with different algorithms and see what effect they have on the accuracy of the results."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "victorian-colonial",
   "metadata": {},
   "source": [
    "## Spider-man\n",
    "\n",
    "In this case study we'll develop a model of Spider-Man swinging from a\n",
    "springy cable of webbing attached to the top of the Empire State\n",
    "Building. Initially, Spider-Man is at the top of a nearby building, as\n",
    "shown in this figure:\n",
    "\n",
    "![Diagram of the initial state for the Spider-Man case\n",
    "study.](https://github.com/AllenDowney/ModSim/raw/main/figs/spiderman.png)\n",
    "\n",
    "The origin, `O`, is at the base of the Empire State Building. The vector `H` represents the position where the webbing is attached to the building, relative to `O`. The vector `P` is the position of Spider-Man relative to `O`. And `L` is the vector from the attachment point to Spider-Man.\n",
    "\n",
    "By following the arrows from `O`, along `H`, and along `L`, we can see\n",
    "that\n",
    "\n",
    "```\n",
    "H + L = P\n",
    "```\n",
    "\n",
    "So we can compute `L` like this:\n",
    "\n",
    "```\n",
    "L = P - H\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bizarre-strand",
   "metadata": {},
   "source": [
    "The goals of this case study are:\n",
    "\n",
    "1.  Implement a model of this scenario to predict Spider-Man's\n",
    "    trajectory.\n",
    "\n",
    "2.  Choose the right time for Spider-Man to let go of the webbing in\n",
    "    order to maximize the distance he travels before landing.\n",
    "\n",
    "3.  Choose the best angle for Spider-Man to jump off the building, and\n",
    "    let go of the webbing, to maximize range.\n",
    "\n",
    "We'll use the following parameters:\n",
    "\n",
    "1.  According to the Spider-Man Wiki (<http://modsimpy.com/spider>), Spider-Man weighs 76 kg.\n",
    "\n",
    "2.  Let's assume his terminal velocity is 60 m/s.\n",
    "\n",
    "3.  The length of the web is 100 m.\n",
    "\n",
    "4.  The initial angle of the web is 45° to the left of straight down.\n",
    "\n",
    "5.  The spring constant of the web is 40 N/m when the cord is stretched, and 0 when it's compressed.\n",
    "\n",
    "In the repository for this book, you will find a notebook,\n",
    "*spiderman.ipynb*, which contains starter code. \n",
    "You can download it from <https://github.com/AllenDowney/ModSimPy/raw/master/examples/spiderman.ipynb> or run it on Colab at <https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/examples/spiderman.ipynb>.\n",
    "\n",
    "Read through the\n",
    "notebook and run the code. It uses `minimize`, which is a SciPy function that can search for an optimal set of parameters (as contrasted with `minimize_scalar`, which can only search along a single axis)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "african-relative",
   "metadata": {},
   "source": [
    "## Kittens\n",
    "\n",
    "If you have used the Internet, you have probably seen videos of kittens unrolling toilet paper.\n",
    "And you might have wondered how long it would take a standard kitten to unroll 47 m of paper, the length of a standard roll.\n",
    "\n",
    "The interactions of the kitten and the paper rolls are complex.  To keep things simple, let's assume that the kitten pulls down on the free end of the roll with constant force.  And let's neglect the friction between the roll and the axle.\n",
    "\n",
    "This diagram shows the paper roll with the force applied by the kitten, $F$, the lever arm of the force around the axis of rotation, $r$, and the resulting torque, $\\tau$.\n",
    "\n",
    "![Diagram of a roll of toilet paper, showing a force, lever arm, and the resulting torque.](https://github.com/AllenDowney/ModSim/raw/main/figs/kitten.png)\n",
    "\n",
    "Assuming that the force applied by the kitten is 0.002 N, how long would it take to unroll a standard roll of toilet paper?\n",
    "\n",
    "In the repository for this book, you will find a notebook,\n",
    "*kitten.ipynb*, which contains starter code for this case study. Use it to implement this model and check whether the results seem plausible.\n",
    "You can download it from <https://github.com/AllenDowney/ModSimPy/raw/master/examples/kitten.ipynb> or run it on Colab at <https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/examples/kitten.ipynb>."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "future-burlington",
   "metadata": {},
   "source": [
    "## Simulating a Yo-yo\n",
    "\n",
    "Suppose you are holding a yo-yo with a length of string wound around its axle, and you drop it while holding the end of the string stationary. As gravity accelerates the yo-yo downward, tension in the string exerts a force upward. Since this force acts on a point offset from the center of mass, it exerts a torque that causes the yo-yo to spin.\n",
    "\n",
    "The following diagram shows the forces on the yo-yo and the resulting torque. The outer shaded area shows the body of the yo-yo. The inner shaded area shows the rolled up string, the radius of which changes as the yo-yo unrolls.\n",
    "\n",
    "![Diagram of a yo-yo showing forces due to gravity and tension in the\n",
    "string, the lever arm of tension, and the resulting\n",
    "torque.](https://github.com/AllenDowney/ModSim/raw/main/figs/yoyo.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "turkish-result",
   "metadata": {},
   "source": [
    "In this system, we can't figure out the linear and angular acceleration independently; we have to solve a system of equations: \n",
    "\n",
    "$$\\begin{aligned}\n",
    "\\sum F &= m a \\\\\n",
    "\\sum \\tau &= I \\alpha\\end{aligned}$$ \n",
    "\n",
    "where the summations indicate that we are adding up forces and torques.\n",
    "\n",
    "As in the previous examples, linear and angular velocity are related\n",
    "because of the way the string unrolls:\n",
    "\n",
    "$$\\frac{dy}{dt} = -r \\frac{d \\theta}{dt}$$ \n",
    "\n",
    "In this example, the linear and angular accelerations have opposite sign. As the yo-yo rotates counter-clockwise, $\\theta$ increases and $y$, which is the length of the rolled part of the string, decreases."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "surrounded-quilt",
   "metadata": {},
   "source": [
    "Taking the derivative of both sides yields a similar relationship\n",
    "between linear and angular acceleration:\n",
    "\n",
    "$$\\frac{d^2 y}{dt^2} = -r \\frac{d^2 \\theta}{dt^2}$$ \n",
    "\n",
    "Which we can write more concisely: \n",
    "\n",
    "$$a = -r \\alpha$$ \n",
    "\n",
    "This relationship is not a general law of nature; it is specific to scenarios like this where one object rolls along another without stretching or slipping.\n",
    "\n",
    "Because of the way we've set up the problem, $y$ actually has two\n",
    "meanings: it represents the length of the rolled string and the height\n",
    "of the yo-yo, which decreases as the yo-yo falls. Similarly, $a$\n",
    "represents acceleration in the length of the rolled string and the\n",
    "height of the yo-yo.\n",
    "\n",
    "We can compute the acceleration of the yo-yo by adding up the linear\n",
    "forces: \n",
    "\n",
    "$$\\sum F = T - mg = ma$$ \n",
    "\n",
    "Where $T$ is positive because the tension force points up, and $mg$ is negative because gravity points down."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "finnish-disaster",
   "metadata": {},
   "source": [
    "Because gravity acts on the center of mass, it creates no torque, so the only torque is due to tension: \n",
    "\n",
    "$$\\sum \\tau = T r = I \\alpha$$ \n",
    "\n",
    "Positive (upward) tension yields positive (counter-clockwise) angular\n",
    "acceleration.\n",
    "\n",
    "Now we have three equations in three unknowns, $T$, $a$, and $\\alpha$,\n",
    "with $I$, $m$, $g$, and $r$ as known parameters. We could solve these equations by hand, but we can also get SymPy to do it for us:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "crude-tribune",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import symbols, Eq, solve\n",
    "\n",
    "T, a, alpha, I, m, g, r = symbols('T a alpha I m g r')\n",
    "eq1 = Eq(a, -r * alpha)\n",
    "eq2 = Eq(T - m*g, m * a)\n",
    "eq3 = Eq(T * r, I * alpha)\n",
    "soln = solve([eq1, eq2, eq3], [T, a, alpha])\n",
    "soln"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "insured-indie",
   "metadata": {},
   "source": [
    "The results are \n",
    "\n",
    "$$\\begin{aligned}\n",
    "T      &= m g I / I^*   \\\\\n",
    "a      &= -m g r^2 / I^* \\\\\n",
    "\\alpha &= m g r / I^*    \\\\\\end{aligned}$$ \n",
    "\n",
    "where $I^*$ is the augmented moment of inertia, $I + m r^2$. \n",
    "We can use these equations for $a$ and $\\alpha$ to write a slope function and simulate this system.\n",
    "\n",
    "In the repository for this book, you will find a notebook, *yoyo.ipynb*, which contains starter code you can use to implement and test this model.\n",
    "You can download it from <https://github.com/AllenDowney/ModSimPy/raw/master/examples/yoyo.ipynb> or run it on Colab at <https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/examples/yoyo.ipynb>."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "professional-meeting",
   "metadata": {},
   "source": [
    "## Congratulations\n",
    "\n",
    "With that, you have reached the end of the book, so congratulations! I\n",
    "hope you enjoyed it and learned a lot. I think the tools in this book\n",
    "are useful, and the ways of thinking are important, not just in\n",
    "engineering and science, but in practically every field of inquiry.\n",
    "\n",
    "Models are the tools we use to understand the world: if you build good\n",
    "models, you are more likely to get things right. Good luck!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "leading-paint",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
